﻿Imports DevExpress.XtraEditors

Public Class frmUser
    Dim data As DataTable

    Public Sub loadUser()
        Try
            data = mysql_query("SELECT * FROM tb_user ORDER BY user ASC")
            GridControlUser.DataSource = data
            If data.Rows.Count > 0 Then
                SimpleButtonEdit.Enabled = True
                SimpleButtonDelete.Enabled = True
            Else
                SimpleButtonEdit.Enabled = False
                SimpleButtonDelete.Enabled = False
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub frmUser_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        loadUser()
    End Sub

    Private Sub SimpleButtonDelete_Click(sender As Object, e As EventArgs) Handles SimpleButtonDelete.Click
        If XtraMessageBox.Show("Are you sure want to delete this user?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
            Cursor = Cursors.WaitCursor
            Dim id As String = GridView1.GetFocusedRowCellDisplayText("id").ToString
            mysql_non_query(String.Format("DELETE FROM tb_user WHERE id = '{0}'", id))
            loadUser()
            Cursor = Cursors.Default
        End If
    End Sub

    Private Sub GridView1_CustomUnboundColumnData(sender As Object, e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs) Handles GridView1.CustomUnboundColumnData
        If e.Column.AbsoluteIndex = 0 Then
            e.Value = e.ListSourceRowIndex + 1
        End If
    End Sub

    Private Sub SimpleButtonAdd_Click(sender As Object, e As EventArgs) Handles SimpleButtonAdd.Click
        frmUser_Editor.Dispose()
        frmUser_Editor.is_edit = False
        frmUser_Editor.ShowDialog()
    End Sub

    Private Sub SimpleButtonEdit_Click(sender As Object, e As EventArgs) Handles SimpleButtonEdit.Click
        Try
            frmUser_Editor.Dispose()
            frmUser_Editor.is_edit = True
            frmUser_Editor.id = CType(GridView1.GetFocusedRowCellDisplayText("id").ToString, Long)
            frmUser_Editor.ShowDialog()
        Catch ex As Exception
            XtraMessageBox.Show("Please Select One Item For Editing!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub SimpleButtonRefresh_Click(sender As Object, e As EventArgs) Handles SimpleButtonRefresh.Click
        loadUser()
    End Sub
End Class